var app = getApp();
var webhost = app.globalData.webhost;
var util = require('../../utils/util.js');
var utilDate = require('../../utils/date.js');

Page({
  data: {
    index: 0,
    dateStart: '申请日期起',
    dateEnd: '止',
    status: '',
    statusList: ['全部'],
    statusCodeList: [''],
    stateCode: '',
    driverAccount: '',
    sessionid: '',
    pageNumber: 1,
    pageSize: 5,
    codeListKey: 'global_status',
    dataList: [],
    loading:0
  },

  bindStartChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateStart: date,
      dataList:[],
      pageNumber:1,
      loading: 0,

    })
    that.queryFeeApplication()
  },

  bindEndChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateEnd: date,
      dataList: [],
      pageNumber: 1,
      loading: 0,
    })
    that.queryFeeApplication()
  },

  bindStatusChange: function(e) {
    var that = this;
    var index = e.detail.value;
    var stateCode = that.data.statusCodeList[index];
    var status = that.data.statusList[index];
    that.setData({
      index: index,
      stateCode: stateCode,
      // status: status,
      dataList: [],
      pageNumber: 1,
      loading: 0,
    })
    that.queryFeeApplication()
  },

  goToDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './feeAppDetail?id=' + id,
    })
  },

  queryFeeApplication: function() {
    var that = this;
    var myDate = new Date();
    var myDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);
    var date = utilDate.Format('yyyy-MM-dd', myDate);
    var date2 = utilDate.Format('yyyy-MM-dd', myDate2);
    var dateStart = that.data.dateStart == '申请日期起' ? date2 : that.data.dateStart;
    var dateEnd = that.data.dateEnd == '止' ? date : that.data.dateEnd;
    var status = that.data.stateCode;
    var pageNumber = that.data.pageNumber;
    var pageSize = that.data.pageSize;
    var driverAccount = app.globalData.driverAccount;
    var sessionid = app.globalData.sessionid;
    var stateCode = that.data.stateCode;
    console.log(stateCode)
    var parameters = [{
      String: {
        driverAccount: driverAccount,
        sessionid: sessionid,
        stateCode: stateCode,
        dateStart: dateStart,
        dateEnd: dateEnd,
        pageNumber: pageNumber,
        pageSize: pageSize,
        status: status,
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'queryFeeApplication', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(parameters)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        var code = res.data.code;
        if (code == '000000') {
          if(res.data.dataList.length > 0){
            var dataList = res.data.dataList;
            var newList = util.number_in_objOfList_toF2(dataList);
            var claimTypeList = that.data.claimTypeList;
            var claimTypeCodeList = that.data.claimTypeCodeList;
            //将dataList中的费用类型转换为中文
            // if (dataList.length > 0){
            //   for (var i = 0; i < dataList.length; i++){
            //     if (claimTypeCodeList.length > 0){
            //       for (var j = 0; j < claimTypeCodeList.length;j++){
            //         if (claimTypeCodeList[j] == dataList[i].reimburseProject){
            //           dataList[i].reimburseProject = claimTypeList[j];
            //           break;
            //         }
            //       }
            //     }
            //   }
            // }
            that.setData({
              dataList: that.data.dataList.concat(dataList),
              loading: 0,
            })
          } else{
            that.setData({
              loading: 2,
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            confirmColor: '#118EDE',
            content: res.data.msg,
            success: function(res) {
              if (code == '003000') {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../login/login'
                  })
                }
              }
            }
          })
        }
      }
    })
  },

  onLoad: function() {
    var that = this;
    //拉取费用类型
    // wx.request({
    //   url: webhost,
    //   data: {
    //     eap_username: "driver",
    //     eap_password: "Driver2018",
    //     boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
    //     methodName: 'getCodeList', //调用的具体方法
    //     returnType: "json", //返回参数类型
    //     parameters: "[{ String: { 'codeListKey': 'tsp_claimType' } }]"
    //   },
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   method: 'POST',
    //   success: function (res) {
    //     var code = res.data.code;
    //     var claimTypeList = [];
    //     var claimTypeCodeList = [];
    //     if (code == '000000') {
    //       for (var i = 0; i < res.data.codeList.length; i++) {
    //         claimTypeList[i] = res.data.codeList[i].codeName
    //         claimTypeCodeList[i] = res.data.codeList[i].codeValue
    //       }
    //       that.setData({
    //         claimTypeList: claimTypeList,
    //         claimTypeCodeList: claimTypeCodeList
    //       })
    //     } else {
    //       wx.showModal({
    //         title: '提示',
    //         showCancel: false,
    //         confirmColor: '#118EDE',
    //         content: res.data.msg,
    //         success: function (res) {
    //           if (res.confirm) { }
    //         }
    //       })
    //     }
    //   },
    //   fail: function (res) {
    //     wx.showModal({
    //       title: '提示',
    //       showCancel: false,
    //       confirmColor: '#118EDE',
    //       content: '服务器异常',
    //       success: function (res) {
    //         if (res.confirm) { }
    //       }
    //     })
    //   }
    // })
    //获取审批状态  getCodeList
    var codeListKey = that.data.codeListKey;
    var codeList = [{
      String: {
        codeListKey: codeListKey
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'getCodeList', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(codeList)

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        var code = res.data.code;
        if (code == '000000') {
          var index = that.data.index;
          var statusList = [];
          var statusCodeList = [];
          for (var i = 0; i < res.data.codeList.length; i++) {
            statusList[i] = res.data.codeList[i].codeName;
            statusCodeList[i] = res.data.codeList[i].codeValue;
          }
          that.setData({
            statusList: that.data.statusList.concat(statusList),
            statusCodeList: that.data.statusCodeList.concat(statusCodeList),
            // stateCode: statusCodeList[0],
            // status: statusCodeList[0],
          })
          that.queryFeeApplication();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  onShow: function() {

  },

  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      loading: 0,
      pageNumber: 1,
      dataList:[],

    })
    setTimeout(() => {
      that.queryFeeApplication();
    }, 300);
  },

  onReachBottom() {
    var that = this;
    if (that.data.loading == 0) {
      that.setData({
        loading: true,
        pageNumber: that.data.pageNumber + 1
      })
      setTimeout(() => {
        that.queryFeeApplication();
      }, 300);
    }
  },

  onShareAppMessage: function() {

  }
})